import vv from "../../Common/vv";
import GameDataInstance, { player } from "../../Common/GameDataInstance";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class LGRankingview extends cc.Component {

   
    // onLoad () {}


    //排行榜panel
    @property(cc.Node)
    rankingPanel:cc.Node = null


    //返回按钮
    @property(cc.Node)
    returnBtn:cc.Node = null

    //我的排名
    @property(cc.Node)
    myrank:cc.Node = null

    //我的头像
    @property(cc.Node)
    myicon:cc.Node = null

    //我的名字
    @property(cc.Node)
    myname:cc.Node = null

    //我的分数
    @property(cc.Node)
    myscore:cc.Node = null



    //横向排行
   @property(cc.Node)
   rank1:cc.Node = null

   @property(cc.Node)
   icon1:cc.Node = null

   @property(cc.Node)
   namestr1:cc.Node = null

   @property(cc.Node)
   score1:cc.Node = null

  

   //横向排行
   @property(cc.Node)
   rank2:cc.Node = null

   @property(cc.Node)
   icon2:cc.Node = null

   @property(cc.Node)
   namestr2:cc.Node = null

   @property(cc.Node)
   score2:cc.Node = null

   
   //横向排行
   @property(cc.Node)
   rank3:cc.Node = null

   @property(cc.Node)
   icon3:cc.Node = null

   @property(cc.Node)
   namestr3:cc.Node = null

   @property(cc.Node)
   score3:cc.Node = null

   //世界排行on
   @property(cc.Node)
   worldpaihangOn:cc.Node = null

   //世界排行off
   @property(cc.Node)
   worldpaihangOff:cc.Node = null

   //本地排行ON
   @property(cc.Node)
   localpaihangOn:cc.Node = null

   //本地排行Off
   @property(cc.Node)
   localpaihangOff:cc.Node = null

   //分享到头条
   @property(cc.Node)
   share:cc.Node = null




    start () {

        vv.btnClick(this.share,()=>
        {
            tt.shareAppMessage(function()
            {
                return {
                    title: GameDataInstance.Instance.titie,
                    imageUrl: GameDataInstance.Instance.thumb,
                }
            })
        })

       vv.btnClick(this.worldpaihangOff,()=>
       {
        this.requestui(GameDataInstance.Instance.worldpaihangdata)
        // let data = GameDataInstance.Instance.worldpaihangdata
        // this.myrank.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.rank
        // vv.SetWeChatImageByWebUrl(GameDataInstance.Instance.myworldpaihangdata.avatar,this.myicon)
        // if(GameDataInstance.Instance.myworldpaihangdata.nickname.length>7)
        // {
        //     this.myname.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname.substring(0,6)+'...'
        // }
        // else
        // {
        //     this.myname.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname
        // }
        // this.myscore.getComponent(cc.Label).string = (Number)(GameDataInstance.Instance.myworldpaihangdata.credit1).toString()

        // this.rank2.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.rank
        // vv.SetWeChatImageByWebUrl(GameDataInstance.Instance.myworldpaihangdata.avatar,this.icon2)
        // if(GameDataInstance.Instance.myworldpaihangdata.nickname.length>7)
        // {
        //     this.namestr2.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname.substring(0,6)+'...'
        // }
        // else
        // {
        //     this.namestr2.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname
        // }
        // this.score2.getComponent(cc.Label).string = (Number)(GameDataInstance.Instance.myworldpaihangdata.credit1).toString()


        // vv.instantiateItem(this.rankingPanel,data.length,()=>
        // {
        //     for(let i =0;i<data.length;i++)
        //     {
        //         if(data[i].uid == GameDataInstance.Instance.worldpaihangdata.uid)
        //         {
        //             this.rank1.getComponent(cc.Label).string = data[i-1].rank
        //             vv.SetWeChatImageByWebUrl(data[i-1].avatar,this.icon1)
        //             this.namestr1.getComponent(cc.Label).string = data[i-1].nickname
        //             this.score1.getComponent(cc.Label).string = (Number)(data[i-1].credit1).toString()

        //             this.rank3.getComponent(cc.Label).string = data[i+1].rank
        //             vv.SetWeChatImageByWebUrl(data[i+1].avatar,this.icon3)
        //             this.namestr3.getComponent(cc.Label).string = data[i+1].nickname
        //             this.score3.getComponent(cc.Label).string = (Number)(data[i+1].credit1).toString()
        //         }
        //         let rankItem = this.rankingPanel.children[i]
        //         rankItem.getComponent('LgRankItem').setData(data[i],GameDataInstance.Instance.myworldpaihangdata)
        //     }
        // })
           this.worldpaihangOff.active = false
           this.worldpaihangOn.active = true
           this.localpaihangOff.active = true
           this.localpaihangOn.active = false


       })

       vv.btnClick(this.localpaihangOff,()=>
       {

        this.requestui(GameDataInstance.Instance.localpaihangdata)
        // let data = GameDataInstance.Instance.localpaihangdata
        // this.myrank.getComponent(cc.Label).string = GameDataInstance.Instance.mylocalpaihangdata.rank
        // vv.SetWeChatImageByWebUrl(GameDataInstance.Instance.mylocalpaihangdata.avatar,this.myicon)
        // if(GameDataInstance.Instance.myworldpaihangdata.nickname.length>7)
        // {
        //     this.myname.getComponent(cc.Label).string = GameDataInstance.Instance.mylocalpaihangdata.nickname.substring(0,6)+'...'
        // }
        // else
        // {
        //     this.myname.getComponent(cc.Label).string = GameDataInstance.Instance.mylocalpaihangdata.nickname
        // }
        // this.myscore.getComponent(cc.Label).string = (Number)(GameDataInstance.Instance.mylocalpaihangdata.credit1).toString()

        // this.rank2.getComponent(cc.Label).string = GameDataInstance.Instance.mylocalpaihangdata.rank
        // vv.SetWeChatImageByWebUrl(GameDataInstance.Instance.mylocalpaihangdata.avatar,this.icon2)
        // if(GameDataInstance.Instance.myworldpaihangdata.nickname.length>7)
        // {
        //     this.namestr2.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname.substring(0,6)+'...'
        // }
        // else
        // {
        //     this.namestr2.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname
        // }
        // this.score2.getComponent(cc.Label).string = (Number)(GameDataInstance.Instance.mylocalpaihangdata.credit1).toString()


        // vv.instantiateItem(this.rankingPanel,data.length,()=>
        // {
        //     for(let i =0;i<data.length;i++)
        //     {
        //         if(data[i].uid == GameDataInstance.Instance.localpaihangdata.uid)
        //         {
        //             this.rank1.getComponent(cc.Label).string = data[i-1].rank
        //             vv.SetWeChatImageByWebUrl(data[i-1].avatar,this.icon1)
        //             this.namestr1.getComponent(cc.Label).string = data[i-1].nickname
        //             this.score1.getComponent(cc.Label).string = (Number)(data[i-1].credit1).toString()

        //             this.rank3.getComponent(cc.Label).string = data[i+1].rank
        //             vv.SetWeChatImageByWebUrl(data[i+1].avatar,this.icon3)
        //             this.namestr3.getComponent(cc.Label).string = data[i+1].nickname
        //             this.score3.getComponent(cc.Label).string = (Number)(data[i+1].credit1).toString()
        //         }
        //         let rankItem = this.rankingPanel.children[i]
        //         rankItem.getComponent('LgRankItem').setData(data[i],GameDataInstance.Instance.mylocalpaihangdata)
        //     }
        // }) 
        this.worldpaihangOff.active = true
        this.worldpaihangOn.active = false
        this.localpaihangOff.active = false
        this.localpaihangOn.active = true
       })

        vv.btnClick(this.returnBtn,()=>
        {
            this.node.destroy()
        })

    }



    //刷新数据
    requestui(data:any)
    {

        this.myrank.getComponent(cc.Label).string = GameDataInstance.Instance.mylocalpaihangdata.rank
        vv.SetWeChatImageByWebUrl(GameDataInstance.Instance.mylocalpaihangdata.avatar,this.myicon)
        if(GameDataInstance.Instance.mylocalpaihangdata.nickname.length>7)
        {
            this.myname.getComponent(cc.Label).string = GameDataInstance.Instance.mylocalpaihangdata.nickname.substring(0,6)+'...'
        }
        else
        {
            this.myname.getComponent(cc.Label).string = GameDataInstance.Instance.mylocalpaihangdata.nickname
        }
        this.myscore.getComponent(cc.Label).string = (Number)(GameDataInstance.Instance.mylocalpaihangdata.credit1).toString()
        
      

        console.log('排行榜数据',data)
        console.log('我的',GameDataInstance.Instance.myworldpaihangdata)
        vv.instantiateItem(this.rankingPanel,data.length,()=>
        {
            for(let i =0;i<data.length;i++)
            {
                if(data[i].uid == GameDataInstance.Instance.myworldpaihangdata.uid)
                {
                   
                    if(i==0)
                    {
                        console.log('查看rank没数据?',GameDataInstance.Instance.myworldpaihangdata.rank)
                        this.rank1.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.rank
                        vv.SetWeChatImageByWebUrl(GameDataInstance.Instance.myworldpaihangdata.avatar,this.icon1)
                        if(GameDataInstance.Instance.myworldpaihangdata.nickname.length>7)
                        {
                            this.namestr1.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname.substring(0,6)+'...'
                        }
                        else
                        {
                            this.namestr1.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname
                        }
                     
                        this.score1.getComponent(cc.Label).string = (Number)(GameDataInstance.Instance.myworldpaihangdata.credit1).toString()
                        console.log('查看data[i+1].rank',data[i+1].rank)
                        this.rank2.getComponent(cc.Label).string = data[i+1].rank
                        vv.SetWeChatImageByWebUrl(data[i+1].avatar,this.icon2)
                        if( data[i+1].nickname.length>7)
                        {
                            this.namestr2.getComponent(cc.Label).string = data[i+1].nickname.substring(0,6)+'...'
                        }
                        else
                        {
                            this.namestr2.getComponent(cc.Label).string = data[i+1].nickname
                        }
                        this.score2.getComponent(cc.Label).string = (Number)(data[i+1].credit1).toString()
                       
                        console.log('查看data[i+2].rank',data[i+2].rank)
                        this.rank3.getComponent(cc.Label).string = data[i+2].rank
                        vv.SetWeChatImageByWebUrl(data[i+2].avatar,this.icon3)
                        if( data[i+2].nickname.length>7)
                        {
                            this.namestr3.getComponent(cc.Label).string = data[i+2].nickname.substring(0,6)+'...'  
                        }
                        else
                        {
                            this.namestr3.getComponent(cc.Label).string = data[i+2].nickname
                        }
                        this.score3.getComponent(cc.Label).string = (Number)(data[i+2].credit1).toString()
                    }

                    else if(data[i+1]==null||data[i+1].rank == undefined)
                    {
                        console.log('查看2222',data[i+2].rank)
                        this.rank1.getComponent(cc.Label).string = data[i-2].rank
                        vv.SetWeChatImageByWebUrl(data[i-2].avatar,this.icon1)
                        if(data[i-2].nickname.length>7)
                        {
                            this.namestr1.getComponent(cc.Label).string = data[i-2].nickname.substring(0,6)+'...'  
                        }
                        else
                        {
                            this.namestr1.getComponent(cc.Label).string = data[i-2].nickname
                        }
                        this.score1.getComponent(cc.Label).string = (Number)(data[i-2].credit1).toString()
    
                        
                        this.rank2.getComponent(cc.Label).string = data[i-1].rank
                        vv.SetWeChatImageByWebUrl(data[i-1].avatar,this.icon2)
                        if(data[i-1].nickname.length>7)
                        {
                            this.namestr2.getComponent(cc.Label).string = data[i-1].nickname.substring(0,6)+'...'  
                        }
                        else
                        {
                            this.namestr2.getComponent(cc.Label).string = data[i-1].nickname
                        }
                        this.score2.getComponent(cc.Label).string = data[i-1].credit1.substring(0,GameDataInstance.Instance.myworldpaihangdata.credit1.length-2)

                        this.rank3.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.rank
                        vv.SetWeChatImageByWebUrl(GameDataInstance.Instance.myworldpaihangdata.avatar,this.icon3)
                        if(GameDataInstance.Instance.myworldpaihangdata.nickname.length>7)
                        {
                            this.namestr3.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname.substring(0,6)+'...'  
                        }
                        else
                        {
                            this.namestr3.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname
                        }
                        this.score3.getComponent(cc.Label).string = (Number)(GameDataInstance.Instance.myworldpaihangdata.credit1).toString()

                        
                    }
                    else
                    {
                        this.rank1.getComponent(cc.Label).string = data[i-1].rank
                        vv.SetWeChatImageByWebUrl(data[i-1].avatar,this.icon1)
                        if(data[i-1].nickname.length>7)
                        {
                            this.namestr1.getComponent(cc.Label).string = data[i-1].nickname.substring(0,6)+'...'    
                        }
                        else
                        {
                            this.namestr1.getComponent(cc.Label).string = data[i-1].nickname
                        }
                        this.score1.getComponent(cc.Label).string = data[i-1].credit1.substring(0,GameDataInstance.Instance.myworldpaihangdata.credit1.length-2)
                        this.rank2.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.rank
                        vv.SetWeChatImageByWebUrl(GameDataInstance.Instance.myworldpaihangdata.avatar,this.icon2)
                        if(GameDataInstance.Instance.myworldpaihangdata.nickname.length>7)
                        {
                            this.namestr2.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname.substring(0,6)+'...'  
                        }
                        else
                        {
                            this.namestr2.getComponent(cc.Label).string = GameDataInstance.Instance.myworldpaihangdata.nickname
                        }
                        this.score2.getComponent(cc.Label).string = (Number)(GameDataInstance.Instance.myworldpaihangdata.credit1).toString()
    
                        this.rank3.getComponent(cc.Label).string = data[i+1].rank
                        vv.SetWeChatImageByWebUrl(data[i+1].avatar,this.icon3)
                        if(data[i+1].nickname.length>7)
                        {
                            this.namestr3.getComponent(cc.Label).string = data[i+1].nickname.substring(0,6)+'...'    
                        }
                        else
                        {
                            this.namestr3.getComponent(cc.Label).string = data[i+1].nickname
                        }
                        this.score3.getComponent(cc.Label).string = (Number)(data[i+1].credit1).toString()
                    }
                }
                let rankItem = this.rankingPanel.children[i]
                rankItem.getComponent('LgRankItem').setData(data[i],GameDataInstance.Instance.myworldpaihangdata)
            }
        })
    }



    // update (dt) {}
}
